<?php
require_once("./define/define_table.php");

define("ADD_OBJECTIF_FORM", "Formulaire d'ajout d'une objectif");
define("MODIFY_OBJECTIF_FORM", "Formulaire de modification d'une objectif");

class Objectif{
	
	private $id_objectif;
	private $id_association;
	private $nom;
	private $est_atteint;
	private $description;
	
	public function Objectif(){
	       
	}
	
	public function getIdObjectif(){
		return $this->id_objectif;
	}
	
	public function getIdAssociation(){
		return $this->id_association;
	}
	
	public function getNom(){
		return $this->nom;
	}
	
	public function getEstAtteint(){
		return $this->est_atteint;
	}
	
	public function getDescription(){
		return $this->description;
	}
	
	public function setIdObjectif($id_objectif){
		$this->id_objectif = $id_objectif;
	}
	
	public function setIdAssociation($id_association){
		$this->id_association = $id_association;
	}
	
	public function setNom($nom){
	   $this->nom = $nom;
	}
	
	public function setEstAtteint($est_atteint){
		$this->est_atteint = $est_atteint;
	}
	
	public function setDescription($description){
		$this->description = $description;
	}
	
	
	public function getAllObjectif(){
	    global $db;
		try{
			$sql = "SELECT id_objectif, id_association, nom, est_atteint, description FROM ".OBJECTIF_TABLE;
			$sth = $db->prepare($sql);
			$sth->execute();
			PrintQuery::queryPrint($sth);
			return $sth->fetchAll(PDO::FETCH_CLASS, "Objectif");
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public function getObjectif($id){
	    global $db;
	    try{
	        $sql = "SELECT id_objectif, id_association, nom, est_atteint, description FROM ".OBJECTIF_TABLE." WHERE id_objectif = :id_objectif";
	        $sth = $db->prepare($sql);
	        $sth->bindValue(":id_objectif", $id, PDO::PARAM_INT);
	        $sth->execute();
	        return $sth->fetchObject("Objectif");
	    }catch(PDOException $e){
	        echo $e.getMessage();
	    }
	}
	
	public function addOrModify(){
		global $db;
		try{
			if($this->id_objectif == null){
				$sql = "INSERT INTO ".OBJECTIF_TABLE." VALUES(null, :nom, :id_association, :est_atteint, :description)";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
				$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
				$sth->bindValue(":est_atteint", $this->est_atteint, PDO::PARAM_BOOL);
				$sth->bindValue(":description", $this->description, PDO::PARAM_STR);
			}
			else{
				$sql = "UPDATE ".OBJECTIF_TABLE." 
						SET nom = :nom, 
						id_association = :id_association, 
						est_atteint = :est_atteint, 
						description = :description 
						WHERE id_objectif= :id_objectif";
				$sth = $db->prepare($sql);
				$sth->bindValue(":nom", $this->nom, PDO::PARAM_STR);
				$sth->bindValue(":id_association", $this->id_association, PDO::PARAM_INT);
				$sth->bindValue(":est_atteint", $this->est_atteint, PDO::PARAM_BOOL);
				$sth->bindValue(":description", $this->description, PDO::PARAM_STR);
				$sth->bindValue(":id_objectif", $this->id_objectif, PDO::PARAM_INT);
			}
			PrintQuery::queryPrint($sth);
			return $sth->execute(); 
		}catch(PDOException $e){
			echo $e.getMessage();
		}
	}
	
	public static function getAddForm(){
		$html = ADD_OBJECTIF_FORM .
				'<form class="ajax_form" id="add_objectif_form" action="?module=objectif&action=doAdd" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._NOM_OBJECTIF_.'</td>' .
				'			<td><input type="text" name="nom" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ID_ASSOCIATION_.'</td>' .
				'			<td>'.Association::generateSelect().'</td>' .
				'		</tr>'. 
				'		<tr>' .
				'			<td>'._DESCRIPTION_.'</td>' . 
				'			<td><input type="text" name="description" /></td>' .
				'		</tr>'.
				'		<tr>' .
				'			<td>'._EST_ATTEINT_.'</td>' . 
				'			<td>'.self::selectAtteint().'</td>' .
				'		</tr>'.
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}
	
	public function getModifyForm(){
		$html = MODIFY_OBJECTIF_FORM .
				'<form class="ajax_form" id="modify_objectif_form" action="?module=objectif&action=doModify" method="post">' .
				'	<table>' .
				'		<tr>' .
				'			<td>'._ID_OBJECTIF_.'</td>' .
				'			<td><input readonly type="text" name="id_objectif" value="'.$this->id_objectif.'"/></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._NOM_OBJECTIF_.'</td>' .
				'			<td><input type="text" name="nom" value="'.$this->nom.'" /></td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td>'._ID_ASSOCIATION_.'</td>' .
				'			<td>'.Association::generateSelect($this->id_association).'</td>' .
				'		</tr>'. 
				'		<tr>' .
				'			<td>'._DESCRIPTION_.'</td>' . 
				'			<td><input type="text" name="description" value ="'.$this->description.'"/></td>' .
				'		</tr>'.
				'		<tr>' .
				'			<td>'._EST_ATTEINT_.'</td>' . 
				'			<td>'.self::selectAtteint($this->est_atteint).'</td>' .
				'		</tr>' .
				'		<tr>' .
				'			<td colspan="2"><input type="submit" />' .
				'		</tr>' .
				'	</table>' .
				'</form>';
		return $html;
	}

	public function delete(){
	    global $db;
	    $sql = "DELETE FROM ".OBJECTIF_TABLE." WHERE id_objectif = :id_objectif";
	    $sth = $db->prepare($sql);
	    $sth->bindValue(":id_objectif", $this->id_objectif, PDO::PARAM_INT);
	    $sth->execute();
	    PrintQuery::queryPrint($sth);
	}
	
	public function selectAtteint($bool = null){
	    $selected1 = ($bool == true) ? 'selected="selected"' : '';
	    $selected0 = ($bool == false) ? 'selected="selected"' : '';
	    $html = '<select name="est_atteint">
	    			<option value="1" '.$selected1.'>'._YES_.'</option>
	    			<option value="0" '.$selected0.'>'._NO_.'</option>
	    		</select>';
	   return $html;
	}
}
?>
